package example.Fadenkreuz.fancy;
import java.awt.*;
import java.awt.event.*;

import javax.media.opengl.*;

import com.sun.opengl.util.Animator;



public class MyFirst  implements WindowListener, GLEventListener, MouseMotionListener, MouseListener {
	
	private GLCanvas canvas;		//OpenGL-Canvas
	double left = -10, right = 10;	//Koordinatenbereich
	double bottom, top;
	double near = -10;				//Clipping Bereich
	double far = 10;
	double mx = 0;
	double my = 0;
	
	private void zeichneAchsen(GL gl){	//Koordinatenachsen zeichnen
		gl.glBegin(GL.GL_LINES);
		gl.glVertex2d(left, 0);
		gl.glVertex2d(right, 0);
		gl.glVertex2d(0, bottom);
		gl.glVertex2d(0, top);
		gl.glEnd();
	}
	
	private void zeichneCrosshair(GL gl){
		gl.glBegin(GL.GL_LINES);
		gl.glVertex2d(this.mx-1, this.my);
		gl.glVertex2d(this.mx+1, this.my);
		gl.glVertex2d(this.mx, this.my-1);
		gl.glVertex2d(this.mx, this.my+1);
		gl.glEnd();
	}
	
	private void zeichneKreis(GL gl, double xm, double ym, double r, int nPkte){
		double phi = 2*Math.PI/nPkte;
		gl.glBegin(GL.GL_POLYGON);
		for(int i=0; i < nPkte; i++){
			gl.glVertex2d(xm + r * Math.cos(i * phi), ym + r*Math.sin(i * phi));
		}
		gl.glEnd();
	}
	
	public MyFirst(){
		Frame f = new Frame("MyFirst");
		canvas = new GLCanvas();
		f.setSize(480, 320);
		f.setBackground(Color.gray);
		f.addWindowListener(this);
		canvas.addGLEventListener(this);
		canvas.addMouseMotionListener(this);
//		canvas.addMouseListener(this);
		f.add(canvas);
		f.setVisible(true);
		Animator an = new Animator();
		an.add(canvas);
		an.start();
	}
	
	public static void main(String[] args){
		new MyFirst();
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		final GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glColor3d(0, 1, 1);
		gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
		zeichneAchsen(gl);
		zeichneKreis(gl, this.mx, this.my, 3, 40);
		zeichneKreis(gl, this.mx, this.my, 1.5, 40);
		zeichneCrosshair(gl);
		
	}

	@Override
	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

	@Override
	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height) {
		GL gl = drawable.getGL();
		double yxRatio = (float)height/width;
		bottom = yxRatio * left;
		top = yxRatio * right;
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrtho(left, right, bottom, top, near, far);
//		gl.glOrtho(canvas.getX(), canvas.getX()+canvas.getWidth(), canvas.getY()-canvas.getHeight(), canvas.getY(), near, far);
//		System.out.println("canvas coordinates: "+canvas.getX()+" "+canvas.getY()+" "+canvas.getWidth()+" "+canvas.getHeight());
		gl.glViewport(0, 0, width, height);
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		NormalizeCoordinates(e);
		canvas.display();
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		NormalizeCoordinates(e);
	}
	private void NormalizeCoordinates(MouseEvent e){
		double x = e.getX();
		double y = e.getY();
		//0-Punkt im KoordiantenNetz bestimmen
		double x0 = canvas.getWidth()/2 + canvas.getX();
		double y0 = canvas.getHeight()/2 - canvas.getY();
		System.out.printf("0 Punkte liegt in %f / %f",x0,y0);
		System.out.println();
		double modifierx = 1;
		double modifiery = 1;
//		double lengthratio = (x - x0) / right;
//		double heightratio = (y - y0) / top;
		double lengthratio = right / (canvas.getWidth() - x0);
		double heightratio = top / (canvas.getHeight() - y0);
		double normalizedlength = Math.abs((x-x0) * lengthratio);
		double normalizedheight = Math.abs((y-y0) * heightratio);
		System.out.println();
		System.out.printf("Normalized Length: %f, Normalized height: %f",normalizedlength,normalizedheight);
		if (x < x0) modifierx = -1;
		if (y > y0) modifiery = -1;
		this.mx = 0 + normalizedlength * modifierx;
		this.my = 0 + normalizedheight * modifiery;
		System.out.println("mx: "+ this.mx + " my: "+ this.my);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
}
